//
//  LBDatePickerView.m
//  app
//
//  Created by 赵辉 on 16/7/12.
//  Copyright © 2016年 广东联结电子商务有限公司. All rights reserved.
//

#import "LBDatePickerView.h"
#import <Masonry/Masonry.h>
#define WINDOW         [[UIApplication sharedApplication].delegate window]
static CGFloat const LBDatePickerViewDatePickerTopHeight = 35;

static CGFloat const LBDatePickerViewDatePickerHeight = (216 + LBDatePickerViewDatePickerTopHeight);

@interface LBDatePickerView ()

@property (strong, nonatomic) UIView *datePickerBgView;

@property (nonatomic,strong)UIDatePicker * datePicker;

@property (copy, nonatomic) lbDatePickerViewDateChoseBlock currentChoseBlock;

@property (assign, nonatomic) UIDatePickerMode currentPickerModel;

@end

@implementation LBDatePickerView

+ (instancetype)shareDatePicker
{
    static LBDatePickerView *shareDatePicker = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shareDatePicker = [[LBDatePickerView alloc]initWithFrame:WINDOW.bounds];
        [WINDOW addSubview:shareDatePicker];
        [shareDatePicker setHidden:YES];
    });
    return shareDatePicker;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createAllView];
    }
    return self;
}

- (void)createAllView
{
    [self addSubview:self.datePickerBgView];
    
    [self addTarget:self action:@selector(closeCurrentView) forControlEvents:UIControlEventTouchUpInside];
    
    self.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.4];
 
}

#pragma mark - getter

- (UIView *)datePickerBgView
{
    if (!_datePickerBgView) {
        
        _datePickerBgView = [[UIView alloc]initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width, LBDatePickerViewDatePickerHeight)];
        _datePickerBgView.backgroundColor = [UIColor whiteColor];
        
        UIView *topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,  self.frame.size.width,LBDatePickerViewDatePickerTopHeight)];
        
        topView.backgroundColor = [UIColor colorWithRed:243/255.0 green:244/255.0 blue:245/255.0 alpha:1.0];
        
        UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        
        [cancelButton setTitleColor:[UIColor colorWithRed:92/255.0 green:167/255.0 blue:240/255.0 alpha:1.0] forState:UIControlStateNormal];
        cancelButton.titleLabel.font = [UIFont systemFontOfSize:14];
        
        UILabel *titleLabel = [[UILabel alloc]init];
        [titleLabel setText:@"选择时间"];
        titleLabel.font = [UIFont systemFontOfSize:15];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.textColor = [UIColor lightGrayColor];
        
        UIButton *confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [confirmButton setTitle:@"确定" forState:UIControlStateNormal];
        [confirmButton setTitleColor:[UIColor colorWithRed:92/255.0 green:167/255.0 blue:240/255.0 alpha:1.0] forState:UIControlStateNormal];
        confirmButton.titleLabel.font = [UIFont systemFontOfSize:14];
        
        [_datePickerBgView addSubview:topView];
        
        [topView addSubview:cancelButton];
        [topView addSubview:titleLabel];
        [topView addSubview:confirmButton];
        
        [cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(topView).offset(15);
            make.centerY.mas_equalTo(topView);
        }];
        
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(topView);
            make.centerY.mas_equalTo(topView);
        }];
        
        [confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(topView).offset(-15);
            make.centerY.mas_equalTo(topView);
        }];
        
        [cancelButton addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
        
        [confirmButton addTarget:self action:@selector(confirmActon) forControlEvents:UIControlEventTouchUpInside];
        
        [_datePickerBgView addSubview:self.datePicker];
        
        CGRect newRect = _datePickerBgView.bounds;
        
        newRect.origin.y = LBDatePickerViewDatePickerTopHeight;
        
        newRect.size.height -= LBDatePickerViewDatePickerTopHeight;
        
        [self.datePicker setFrame:newRect];
    }
    return _datePickerBgView;
}

- (UIDatePicker *)datePicker
{
    if (!_datePicker) {
        _datePicker = [[UIDatePicker alloc] init];
        _datePicker.backgroundColor = [UIColor whiteColor];
        [_datePicker setLocale:[NSLocale currentLocale]];
        _datePicker.minimumDate = [NSDate dateWithTimeIntervalSince1970:0];     // 最小值2
        [_datePicker setDate:[NSDate date] animated:YES];    // 设置日期控件值
        
        _datePicker.datePickerMode = UIDatePickerModeDate;               // 日期模式
    }
    return _datePicker;
}

#pragma mark -action

- (void)cancelAction
{
    [self closeCurrentView];
}

- (void)confirmActon
{
    [self cancelAction];
    if (self.currentChoseBlock) {
        self.currentChoseBlock(self.datePicker.date);
    }
}

+ (void)showDataPickerByAnimation:(BOOL)animation
                 UIDatePickerMode:(UIDatePickerMode)pickerModel
                   isLimitMaxTime:(BOOL)isLimitMaxTime
                         Complete:(lbDatePickerViewDateChoseBlock)choseBlock
{
    LBDatePickerView *datePickerView = [self shareDatePicker];
    
    [WINDOW bringSubviewToFront:datePickerView];
    
    datePickerView.datePicker.datePickerMode = pickerModel;
    
    if (isLimitMaxTime) {
        datePickerView.datePicker.maximumDate = [NSDate dateWithTimeIntervalSinceNow:60*60*24*31]; // 最大值
    } else {
        datePickerView.datePicker.maximumDate = nil; //不限制
    }

    [datePickerView setHidden:NO];
    
    datePickerView.currentChoseBlock = choseBlock;
    
    if (datePickerView.datePickerBgView.frame.origin.y != [UIScreen mainScreen].bounds.size.height) {
        CGRect frame = datePickerView.datePickerBgView.frame;
        frame.origin.y = [UIScreen mainScreen].bounds.size.height;
        datePickerView.datePickerBgView.frame = frame;
    }
    __weak typeof(self) weakSelf = self;

    datePickerView.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0];
    [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        datePickerView.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.4];
    
        CGRect frame = datePickerView.datePickerBgView.frame;
        frame.origin.y = [UIScreen mainScreen].bounds.size.height - LBDatePickerViewDatePickerHeight;
        datePickerView.datePickerBgView.frame = frame;
        
    } completion:^(BOOL finished) {
    }];
    
}

+ (void)showDataPickerByAnimation:(BOOL)animation
                 UIDatePickerMode:(UIDatePickerMode)pickerModel
     LBDatePickerViewMaxLimitType:(LBDatePickerViewMaxLimitType)maxLimitType
     LBDatePickerViewMinLimitType:(LBDatePickerViewMinLimitType)minLimitType
                         Complete:(lbDatePickerViewDateChoseBlock)choseBlock
{
    [self showDataPickerByAnimation:animation UIDatePickerMode:pickerModel isLimitMaxTime:YES Complete:choseBlock];
    LBDatePickerView *datePickerView = [self shareDatePicker];
    switch (maxLimitType) {
        case LBDatePickerViewLimitTypeMaxNoLimit: {
            datePickerView.datePicker.maximumDate = nil; //不限制
            break;
        }
        case LBDatePickerViewLimitTypeMaxOverOneMonth: {
            datePickerView.datePicker.maximumDate = [NSDate dateWithTimeIntervalSinceNow:60*60*24*31]; //不限制
            break;
        }
        case LBDatePickerViewLimitTypeMaxCurrentTime: {
            datePickerView.datePicker.maximumDate = [NSDate dateWithTimeIntervalSinceNow:0]; //不限制
            break;
        }
    }
    switch (minLimitType) {
        case LBDatePickerViewMinLimitTypeMinDefault: {
            datePickerView.datePicker.minimumDate = [NSDate dateWithTimeIntervalSince1970:0];
            break;
        }
        case LBDatePickerViewMinLimitTypeMinNoLimit: {
            datePickerView.datePicker.minimumDate = nil;
            break;
        }
        case LBDatePickerViewMinLimitTypeMinNextDay: {
            datePickerView.datePicker.minimumDate = [NSDate dateWithTimeInterval:24*60*60 sinceDate:[NSDate date]];
            break;
        }
        case LBDatePickerViewMinLimitTypeMinCurrentTime: {
            datePickerView.datePicker.minimumDate = [NSDate dateWithTimeIntervalSinceNow:0];
            break;
        }
    }
}

/**
 *  时间选择
 *
 *  @param animation    是否动画
 *  @param pickerModel  时间选择模版
 *  @param maxLimitType 最大时间(无则为nil)
 *  @param minLimitType 最小时间(无则为nil)
 *  @param choseBlock   choseBlock description
 */
+ (void)showDataPickerByAnimation:(BOOL)animation
                 UIDatePickerMode:(UIDatePickerMode)pickerModel
     LBDatePickerViewMaxLimitDate:(NSDate *)maxLimitDate
     LBDatePickerViewMinLimitDate:(NSDate *)minLimitDate
                         Complete:(lbDatePickerViewDateChoseBlock)choseBlock {
    [self showDataPickerByAnimation:animation UIDatePickerMode:pickerModel isLimitMaxTime:YES Complete:choseBlock];
    LBDatePickerView *datePickerView = [self shareDatePicker];
    datePickerView.datePicker.maximumDate = maxLimitDate;
    datePickerView.datePicker.minimumDate = minLimitDate;
}

- (void)closeCurrentView
{
    __weak typeof(self) weakSelf = self;
    CGRect frame = self.datePickerBgView.frame;
    frame.origin.y = [UIScreen mainScreen].bounds.size.height;
    [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        weakSelf.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0];
        weakSelf.datePickerBgView.frame = frame;
    } completion:^(BOOL finished) {
        [weakSelf setHidden:YES];
        weakSelf.currentChoseBlock = nil;
    }];
}

@end
